package com.onlyMe.view.world.objects
{
	import com.flesler.componentas.core.Component;
	import com.flesler.componentas.core.Destroyable;
	import com.onlyMe.view.world.components.ComponentViewCreator;
	import com.onlyMe.world.objects.IsleObject;
	import com.qb9.flashlib.tasks.ITask;
	import com.qb9.flashlib.tasks.TaskRunner;
	
	import flash.display.Sprite;

	public final class IsleObjectViewCreator implements Destroyable
	{
		private var componentCreator:ComponentViewCreator = new ComponentViewCreator();
		private var tasks:TaskRunner;
		
		public function IsleObjectViewCreator(tasks:TaskRunner)
		{
			this.tasks = tasks;
		}
		
		public function createFrom(object:IsleObject, container:Sprite):IsleObjectView
		{
			var view:IsleObjectView = new IsleObjectView(object, container);
			
			for each (var component:Component in object.components)
			{
				var viewComponent:Component = componentCreator.createFrom(component);
				if (viewComponent)
					view.register(viewComponent);
				if (viewComponent is ITask)
					tasks.add(viewComponent as ITask);
			}
			view.initialize();
			return view;
		}
		
		// Destroyable
		
		public function get isDestroyed():Boolean
		{
			return componentCreator === null;
		}
		
		public function destroy():void
		{
			tasks = null;
			componentCreator = null;
		}
	}
}